package com.study.date;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

/**
 * java8时间api
 * */
public class TestLocalDateTime {


    /**
     * 1. LocalDate  LocalTime   LocalDateTime
     * */
    @Test
    public void test1(){
        //获取当前时间
        LocalDateTime ld = LocalDateTime.now();
        System.out.println(ld);
        System.out.println("===========================");
        //获取指定时间
        LocalDateTime ld1 = LocalDateTime.of(2022, 4, 23, 11, 23, 59);
        System.out.println(ld1);
        System.out.println("===========================");
        //获取年月日时分秒
        System.out.println(ld.getYear());
        System.out.println(ld.getMonthValue());
        System.out.println(ld.getDayOfMonth());
        System.out.println(ld.getHour());
        System.out.println(ld.getMinute());
        System.out.println(ld.getSecond());
        System.out.println("===========================");
        //获取之后时间(年月日时分秒)
        LocalDateTime ld2 = ld.plusDays(2);
        System.out.println(ld2);
        System.out.println("===========================");
        //获取之前时间(年月日时分秒)
        LocalDateTime ld3 = ld.minusDays(2);
        System.out.println(ld3);
    }

    /**
     * 2. 计算机时间 instant
     * */
    @Test
    public void test2(){
        //默认是标准时区
        Instant now = Instant.now();
        System.out.println(now);
        System.out.println("===========================");
        //偏移八个小时
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);
        System.out.println("===========================");
        //获取时间戳
        System.out.println(now.toEpochMilli());
    }

    /**
     * 3.时间比较
     * duration
     * */
    @Test
    public void test3() throws Exception{
        //时间比较
        Instant now = Instant.now();
        Thread.sleep(1000);
        Instant now1 = Instant.now();
        Duration between = Duration.between(now,now1);
        System.out.println(between);
        System.out.println(between.toMillis());
        System.out.println("===========================");
        LocalDateTime ld1 = LocalDateTime.now();
        Thread.sleep(1000);
        LocalDateTime ld2 = LocalDateTime.now();
        Duration between1 = Duration.between(ld1, ld2);
        System.out.println(between1);
        System.out.println(between1.toMillis());
    }

    /**
     * 4.日期比较
     * period
     * */
    @Test
    public void test4(){
        //日期比较
        LocalDate now = LocalDate.now();
        LocalDate of = LocalDate.of(2021, 3, 11);
        Period between = Period.between(of, now);
        System.out.println(between);
        System.out.println(between.getYears());
        System.out.println(between.getMonths());
        System.out.println(between.getDays());
    }

    /**
     * 时间校正器
     * TemporalAdjusters
     * */
    @Test
    public void test5(){
        //当前时间
        LocalDateTime ld = LocalDateTime.now();
        System.out.println(ld);
        //这个月10号
        LocalDateTime ld1 = ld.withDayOfMonth(10);
        System.out.println(ld1);
        //下个周日
        LocalDateTime ld2= ld.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
        System.out.println(ld2);
        //这个月第一天
        LocalDateTime ld3 = ld.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(ld3);
        //这个月最后一个周日
        LocalDateTime ld4 = ld.with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));
        System.out.println(ld4);
        //自定义实现下一个工作日
        TemporalAdjuster tem=(l)->{
            LocalDateTime localDate=(LocalDateTime)l;
            DayOfWeek dayOfWeek = localDate.getDayOfWeek();
            if(dayOfWeek.equals(DayOfWeek.FRIDAY)){
                return localDate.plusDays(3);
            }else if(dayOfWeek.equals(DayOfWeek.SATURDAY)){
                return localDate.plusDays(2);
            }else{
                return localDate.plusDays(1);
            }
        };
        LocalDateTime ld5 = ld.with(tem);
        System.out.println(ld5);
    }

    /**
     * 格式化时间/日期
     * DateTimeFormatter
     * */
    @Test
    public void test6(){
        DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE;
        LocalDateTime ld = LocalDateTime.now();
        System.out.println(ld);
        String format = ld.format(isoDateTime);
        System.out.println(format);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        String time = ld.format(formatter);
        System.out.println(time);
        LocalDate date = LocalDate.parse(time, formatter);
        System.out.println(date);
    }

    /**
     * 时区
     * zonedDate,zonedTime,zonedDateTime
     * */
    @Test
    public void test7(){
        //支持的所有时区
        Set<String> set = ZoneId.getAvailableZoneIds();
        set.forEach(System.out::println);
        //指定具体时区
        LocalDateTime now = LocalDateTime.now(ZoneId.of("Africa/Nairobi"));
        System.out.println(now);
    }
}
